﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using OpenExpressApp.MetaAttribute;
using System.Reflection;
using System.Collections;
using OpenExpressApp.Command;
using OpenExpressApp.Types;
using System.Diagnostics;
using OpenExpressApp.MetaModel;
using System.Windows.Media;
using Csla;

namespace OpenExpressApp
{
    /// <summary>
    /// 业务对象的属性元数据
    /// 
    /// AssociationAttribute
    /// </summary>
    public class BusinessObjectsPropertyInfo : IAssociationPropertyInfo, IPropertyViewInfo
    {
        private PropertyInfo _runtimeProperty;

        /// <summary>
        /// 解析元数据
        /// </summary>
        /// <param name="propertyInfo">对应的属性</param>
        /// <param name="attribute">对应的AssociationAttribute</param>
        /// <param name="boInfo">属性所属的类型元数据</param>
        public BusinessObjectsPropertyInfo(PropertyInfo runtimeProperty, AssociationAttribute attribute, BusinessObjectInfo boInfo)
        {
            this._runtimeProperty = runtimeProperty;
            this.BoInfo = boInfo;
            this.AssociationAttribute = attribute;
            //this.ChildShowTypeAttribute = 
            this.Name = runtimeProperty.Name;
            this.PropertyType = runtimeProperty.PropertyType;
            this.BOType = runtimeProperty.PropertyType.BaseType.GetGenericArguments()[1];
            //if (AssociationOperateType.Selected ==  AssociationAttribute.AssociationOperateType)
            //{
            //    MethodInfo method = PropertyType.GetMethod(MethodConvention.GetList_Selected);
            //    Debug.Assert(null != method, String.Format("选择视图对应对象没有实现方法{0}", MethodConvention.GetList_Selected));
            //    this.SelectedBOType = method.ReturnType.BaseType.GetGenericArguments()[1];
            //}

            //考虑BusinessObjectInfo嵌套BusinessObjectInfo情况
            if (boInfo.BOType.Name == BOType.Name)
            {
                Label = boInfo.Label;
            }
            else
            {
                BusinessObjectInfo info = ApplicationModel.GetBusinessObjectInfo(this.BOType);
                if (typeof(IQueryObject).IsAssignableFrom(boInfo.BOType) == false)
                {
                    info.ParentBoInfo = boInfo;
                }
                Label = info.Label;
            }

            if (runtimeProperty.HasMarked<QueryItemValueTypeAttribute>())
            {
                QueryItemValueTypeAttribute = runtimeProperty.GetSingleAttribute<QueryItemValueTypeAttribute>();
            }
            this.IsNavigateQueryItem = runtimeProperty.HasMarked<NavigateQueryItemAttribute>();
            NotCopy = runtimeProperty.HasMarked<NotCopyAttribute>();
            if (runtimeProperty.HasMarked<IsVisibleAttribute>())
            {
                IsVisibleAttribute = runtimeProperty.GetSingleAttribute<IsVisibleAttribute>();
            }
            //子表显示类型
            if (runtimeProperty.HasMarked<ChildShowTypeAttribute>())
            {
                this.ChildShowTypeAttribute = runtimeProperty.GetSingleAttribute<ChildShowTypeAttribute>();
            }
        }

        /// <summary>
        /// 是否可见属性
        /// </summary>
        public IsVisibleAttribute IsVisibleAttribute { get; private set; }

        /// <summary>
        /// 属性名
        /// </summary>
        public string Name { get; private set; }
        /// <summary>
        /// 对应的类型
        /// </summary>
        public Type PropertyType { get; private set; }
        /// <summary>
        /// 对应的类的类型
        /// </summary>
        public Type BOType { get; private set; }

        public string Label { get; private set; }
        public bool IsNavigateQueryItem { get; private set; }
        public QueryItemValueTypeAttribute QueryItemValueTypeAttribute { get; private set; }
        public bool NotCopy { get; private set; }
        /// <summary>
        /// 对应的AssociationAttribute
        /// </summary>
        public AssociationAttribute AssociationAttribute { get; private set; }
        public ChildShowTypeAttribute ChildShowTypeAttribute { get; private set; }
        /// <summary>
        /// 属性所属的类型元数据
        /// </summary>
        public BusinessObjectInfo BoInfo { get; private set; }

        public override string ToString()
        {
            return this.BOType.Name + ":" + this.Name + ":" + base.ToString();
        }

        #region IAssociationPropertyInfo Members

        public PropertyInfo RuntimeProperty
        {
            get
            {
                return this._runtimeProperty;
            }
        }

        IEntityInfo IPropertyInfo.EntityInfo
        {
            get
            {
                return this.BoInfo;
            }
        }

        public IPropertyInfo PropertyInfo
        {
            get
            {
                return this;
            }
        }

        public Type ChildEntityType
        {
            get
            {
                return this.BOType;
            }
        }

        #endregion
    }
}